package calculation

import (
	"math/rand"
	"strconv"
	"time"

	"github.com/saint-yellow/go-pl/toys/sensor-simulator/arguments"
)

var (
	r = rand.New(rand.NewSource(time.Now().UnixNano()))
	Value = r.Float64() * (*arguments.Max - *arguments.Min) + *arguments.Min
	norm = (*arguments.Max - *arguments.Min) / 2 + *arguments.Min
	Duration, _ = time.ParseDuration(strconv.Itoa(1000 / int(*arguments.Frequency)) + "ms")
	Signal = time.Tick(Duration)
)

func CalculateValue() {
	var maxStep, minStep float64
	if Value < norm {
		maxStep = *arguments.StepSize
		minStep = (-1) * *arguments.StepSize * (Value - *arguments.Min) / (norm - *arguments.Min)
	} else {
		maxStep = *arguments.StepSize * (*arguments.Max - Value) / (*arguments.Max - norm)
		minStep = (-1) * *arguments.StepSize
	}

	Value += r.Float64() * (maxStep-minStep) + minStep
}
